package com.hxqc.basic.serivce;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hxqc.basic.common.MD5;
import com.hxqc.basic.common.StringUtil;
import com.hxqc.basic.interceptor.LogAnnotation;
import com.hxqc.basic.mapper.SysUserMapper;
@Transactional
@Service
public class SysUserService {

	@Resource
	private SysUserMapper sysUserMapper;
	
	/**
	 * 通过id查找用户
	 * @param user_id
	 * @return
	 */
	public Map<String, Object> selectSysUserById(String user_id) {
		return sysUserMapper.selectSysUserById(user_id);
	}
	
	/**
	 * 通过用户id查找用户角色
	 * @param user_id
	 * @return
	 */
	public List<String> findSysUserRoleByUserId(String user_id) {
		return sysUserMapper.findSysUserRoleByUserId(user_id);
	}
	
	/**
	 * 通过id查找所有权限
	 * @param user_id
	 * @return
	 */
	public List<Map<String, Object>> findAllRightByUser(String user_id) {
		return sysUserMapper.findAllRightByUser(user_id);
	}
	
	/**
	 * 查找所有权限
	 * @return
	 */
	public List<Map<String, Object>> findAllRights() {
		return sysUserMapper.findAllRights();
	}
	
	/**
	 * 修改管理员密码
	 * @param user
	 */
	@LogAnnotation("修改管理员密码")
	public void updateUserPwd(Map<String, Object> user) {
		sysUserMapper.updateUserPwd(user);
	}
	
	/**
	 * 查找用户信息
	 * @param param
	 * @return
	 */
	public List<Map<String, Object>> findSysUser(Map<String, String> param) {
		return sysUserMapper.findSysUser(param);
	}
	
	/**
	 * 用户数查询
	 * @param param
	 * @return
	 */
	public Integer countSysUser(Map<String, String> param) {
		return sysUserMapper.countSysUser(param);
	}
	
	/**
	 * 查找系统角色
	 * @return
	 */
	public List<Map<String, Object>> findSysRole() {
		return sysUserMapper.findSysRole(null);
	}
	/**
	 * 查找所有部门
	 * @return
	 */
	public List<Map<String, Object>> findSysDept() {
		return sysUserMapper.findSysDept();
	}
	/**
	 * 添加管理员
	 * @param map
	 * @param role_ids
	 */
	@LogAnnotation("添加管理员")
	public void insertIntoSysUser(Map<String, Object> map, String[] role_ids) {
		String password = StringUtil.nvl(map.get("password"));
		if (!password.equals("")) {
			map.put("password", new MD5().getMD5ofStr(password));
		}
		sysUserMapper.insertIntoSysUser(map);
		if (null != role_ids && role_ids.length > 0) {
			for (String role_id : role_ids)
				map.put("role_id", role_id);
			sysUserMapper.insertUserRole(map);
		}
	}
	/**
	 * 编辑系统管理员
	 * @param map
	 * @param role_ids
	 */
	@LogAnnotation("编辑系统管理员")
	public void updateSysUser(Map<String, Object> map, String[] role_ids) {
		String password = StringUtil.nvl(map.get("password"));
		if (!password.equals("")) {
			map.put("password", new MD5().getMD5ofStr(password));
		}
		sysUserMapper.updateSysUser(map);
		if (null == role_ids || role_ids.length > 0) {
			sysUserMapper.deleteUserRole(map.get("user_id") + "");
			for (String role_id : role_ids)
				map.put("role_id", role_id);
			sysUserMapper.insertUserRole(map);
		}
	}
	/**
	 * 删除部门
	 * @param dept_id
	 */
	@LogAnnotation("删除部门")
	public void delDept(String dept_id) {
		sysUserMapper.delDept(dept_id);
	}
	/**
	 * 删除管理员
	 * @param user_id
	 */
	@LogAnnotation("删除管理员")
	public void deleteSysUser(String user_id) {
		sysUserMapper.deleteSysUser(user_id);
		sysUserMapper.deleteUserRole(user_id);
	}
	
	/**
	 * 查找部门
	 * @param dept_code
	 * @return
	 */
	public List<Map<String, Object>> getDeptByCode(String dept_code) {
		return sysUserMapper.getDeptByCode(dept_code);
	}
	
	/**
	 * 查找部门
	 * @param dept_name
	 * @return
	 */
	public List<Map<String, Object>> getDeptByName(String dept_name) {
		return sysUserMapper.getDeptByName(dept_name);
	}

	/**
	 * 添加部门
	 * @param map
	 */
	public void addDept(Map<String, String> map) {
		sysUserMapper.addDept(map);
	}	
	
	/**
	 * 查找角色
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> findSysRole(Map<String, String> map) {
		return sysUserMapper.findSysRole(map);
	}
	
	/**
	 * 角色数
	 * @param map
	 * @return
	 */
	public int countSysRole(Map<String, String> map) {
		return sysUserMapper.countSysRole(map);
	}
	/**
	 * 删除角色
	 * @param role_id
	 */
	@LogAnnotation("删除角色")
	public void deleteRole(String role_id) {
		sysUserMapper.deleteRole(role_id);
		sysUserMapper.deleteUserRoleByRole(role_id);
		sysUserMapper.deleteRoleRight(role_id);
	}
	
	/**
	 * 查找角色信息
	 * @param role_code
	 * @return
	 */
	public Map<String, Object> findRoleInfoByRoleCode(String role_code) {
		return sysUserMapper.findRoleInfoByRoleCode(role_code);
	}
	
	/**
	 * 查找角色信息
	 * @param role_name
	 * @return
	 */
	public Map<String, Object> findRoleInfoByRoleName(String role_name) {
		return sysUserMapper.findRoleInfoByRoleName(role_name);
	}
	
	/**
	 * 查找角色信息
	 * @param role_name
	 * @return
	 */
	public Map<String, Object> findRoleInfoByRoleIdAndName(Map<String, String> map) {
		return sysUserMapper.findRoleInfoByRoleIdAndName(map);
	}
	
	/**
	 * 添加角色
	 * @param map
	 */
	@LogAnnotation("添加角色")
	public void insertRoleInfo(Map<String, Object> map) {
		sysUserMapper.insertRoleInfo(map);
		String right_ids = map.get("right_ids")+"";
		Set<String> rights = new HashSet<String>(Arrays.asList(right_ids.split(",")));
		sysUserMapper.insertSysRoleRight(StringUtil.nvl(map.get("role_id")), rights);
	}
	
	/**
	 * 获得菜单总数
	 * @return
	 */
	public Integer countAllRights() {
		return sysUserMapper.countAllRights();
	}
	
	/**
	 * 查找角色信息
	 * @param role_id
	 * @return
	 */
	public Map<String,Object> findRoleById(String role_id){
		return sysUserMapper.findRoleById(role_id);
	}
	/**
	 * 查找权限信息
	 * @param role_id
	 * @return
	 */
	public List<String> findRightsIdByRoleId(String role_id) {
		return sysUserMapper.findRightsIdByRoleId(role_id);
	}
	
	/**
	 * 编辑角色
	 * @param map
	 */
	@LogAnnotation("编辑角色")
	public void updateRoleInfo(Map<String, String> map) {
		sysUserMapper.updateRoleInfo(map);
		String right_ids = map.get("right_ids");
		Set<String> rights = new HashSet<String>(Arrays.asList(right_ids
					.split(",")));
		sysUserMapper.deleteRoleRight(map.get("role_id"));
		sysUserMapper.insertSysRoleRight(map.get("role_id"), rights);
	}
	/**
	 * 插入登录日志
	 * @param map
	 */
	public void insertLoginLog(Map<String,Object> map) {
		sysUserMapper.insertLoginLog(map);
	}
	/**
	 * 更新用户登出
	 * @param session_id
	 */
	public void updateLogoutLogBySessionId(String session_id) {
		sysUserMapper.updateLogoutLogBySessionId(session_id);
	}
	/**
	 * 更新登出信息
	 */
	public void updateLogoutLog(){
		sysUserMapper.updateLogoutLog();
	}
}
